﻿using AdminSystem.Application;
using AdminSystem.Applications;
using AdminSystem.Domain;
using AdminSystem.WebApi.DTOs;
using AdminSystem.WebApi.MedialRs.Commands.Shopps;
using AdminSystem.WebApi.MedialRs.Commands.TracebilityCommand;
using AdminSystem.WebApi.MedialRs.Commands.UserLogin;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace AdminSystem.WebApi.Controllers
{
    /// <summary>
    /// 溯源控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TracebilityController : ControllerBase
    {
        public TracebilityController(IMediator mediator)
        {
            Mediator = mediator;
        }

        public IMediator Mediator { get; }




        /// <summary>
        /// 中介者添加溯源
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<object>> AddTracebility(TracebilityCommand command)
        {
            return await Mediator.Send(command);
        }
        /// <summary>
        /// 中介者查询溯源
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<ApiPaging<TracebilityDto>>> GetTracebility(int Index,int Size)
        {
            TracebilityQueryCommand command = new TracebilityQueryCommand();
            command.Index = Index;
            command.Size = Size;
            return await Mediator.Send(command);
        }
        /// <summary>
        /// 中介者删除溯源
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<object>> DeleteTracebility(TracebilityDeleteCommand command)
        {
            return await Mediator.Send(command);
        }
        /// <summary>
        /// 修改溯源
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<object>> UpdateTracebility(TracebilityUpdateCommand command)
        {
            return await Mediator.Send(command);
        } 
        
        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<object>> TracebilityExport(string? file)
        {
            TracebilityExportCommand command = new TracebilityExportCommand();
            command.file = file;
            return await Mediator.Send(command);
        }


        /// <summary>
        /// 上传图片的方法
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]

        public IActionResult UploadFile(IFormFile file)
        {
            var fileName=file.FileName;
            var path = Directory.GetCurrentDirectory() + @"\wwwroot\Images\" + fileName;
            using (FileStream fs=new FileStream(path, FileMode.Create))
            {
                file.CopyTo(fs);
            }
            var url="/Images/"+fileName;
            return Ok(url);
        }
    }
}
